<?php
if($this->ma == 1){
    echo '<script language="javascript" type="text/javascript">alert("运单快递价格记录添加成功！");location.href = "/admini/express/prdetail/id/'.$this->priceId.'";</script>';
}
?>
<div class="">
    <form id="formstore" action="/admini/express/addprice/" method="post">
    <div class="admin_nrtop">
        <div class="couriercompanycz">
            <div class="nui-toolbar-item">
                <div class="nui-btn ">
                    <a class="nui-btn-text" href="/admini/express/price"><span>&lt;&lt;</span>返回</a>
                </div>
            </div>
            <div class="nui-toolbar-item">
                <div class="nui-btn ">
                    <a class="nui-btn-text" href="javascript:;" name="save">保存添加</a>
                </div>
                <div class="nui-btn ">
                    <a class="nui-btn-text" href="javascript:;" name="reset">重置</a>
                </div>
            </div>  
        </div>
    </div>
    <div class="addbox">
        <div class="addtop">
            <h1>快递价格设置</h1>
        </div>
        <div class="addtable addtable2">
            <table>
                <tbody><tr>
                    <td class="addl">选择国家：</td>
                    <td>
                        <div class="addressbookguojia">
                            <div class="addressbookinput">
                                <input type="hidden" name="cityId" id="cityId" value="<?php echo $this->cityId ? $this->cityId : 1 ?>"/>
                                <input type="hidden" name="epIdtext" id="epIdtext" value="<?php echo $this->epId ? $this->epId : 1 ?>"/>
                                <input type="text" data="empty" cid="<?php echo $this->cityId ? $this->cityId : 1 ?>" readonly="readonly" value="<?php echo $this->priceInfo ? $this->priceInfo->City->Name : '美国' ?>" id="cityName" name="cityName" placeholder="选择国家" class="txtBxF">
                                <i></i>
                            </div>
                            <div style="display:none;" class="share_guojiaceng">
                                <div class="share_remen">
                                    <h2>热门国家和地区</h2>
                                    <ul class="share_countrylist">
                                        <li>
                                            <a cid="1">美国</a>
                                        </li>
                                        <li>
                                            <a cid="2">加拿大</a>
                                        </li>
                                        <li>
                                            <a cid="3">澳大利亚</a>
                                        </li>
                                        <li>
                                            <a cid="47">日本</a>
                                        </li>
                                        <li>
                                            <a cid="39">台湾</a>
                                        </li>
                                        <li>
                                            <a cid="4">新西兰</a>
                                        </li>
                                        <li>
                                            <a cid="5">英国</a>
                                        </li>
                                        <li>
                                            <a cid="42">马来西亚</a>
                                        </li>
                                    </ul>
                                </div>
                                <div class="share_allcountry">
                                    <h2>全部国家和地区</h2>
                                    <ul class="share_countryzm">
                                        <?php foreach ($this->words as $char) { ?>
                                            <li <?= $char == 'A' ? 'class="share_zmon"' : '' ?>><a href="javascript:;"><?= $char ?></a></li>
                                        <?php } ?>
                                    </ul>
                                    <div class="country_content">
                                    <?php foreach ($this->words as $char) { ?>
                                        <ul class="share_countrylist <?= $char != 'A' ? '' : 'on' ?>" >
                                            <?php
                                                $citys = Application_Model_city::GetBySortCode($char);
                                                foreach ($citys as $city) { ?>
                                                <li><a href="javascript:;" cid="<?= $city->Id ?>"><?= $city->Name ?></a></li>
                                        <?php } ?>
                                        </ul>
                                    <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="addl">快递方式：</td>
                    <td>
                        <div class="addradio">
                            <select id="epId" name="epId">
                            <?php 
                                if($this->expresscomp) {
                                    foreach ($this->expresscomp as $eccomp) { 
                            ?>
                                    <option value="<?php echo $eccomp->id; ?>" <?php echo $this->epId == $eccomp->id ? 'selected="selected"' : '' ?> ><?php echo $eccomp->name; ?></option>
                            <?php 
                                    }
                                } 
                            ?>
                            </select>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="addl">最大限重：</td>
                    <td>
                        <div class="addtian2">
                            <input type="text" datatype="float"  value="<?php echo $this->priceInfo ? $this->priceInfo->Maxkg : '' ; ?>" name="maxkg" id="maxkg" class="txtBxF"><i>kg</i>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="addl">最大限长：</td>
                    <td>
                        <div class="addtian2">
                            <input type="text" datatype="float"  value="<?php echo $this->priceInfo ? $this->priceInfo->Maxcm : '' ; ?>" name="maxcm" id="maxcm" class="txtBxF"><i>cm</i>
                        </div>
                    </td>
                </tr>
            </tbody></table>
        </div>        
        <div class="addtable addtable2">
            <table>
                <tbody><tr>
                    <td class="addl">首重：</td>
                    <td>
                        <div class="shouzhong">
                            <div class="szbox">
                                <input type="text" datatype="float"  value="<?php echo $this->priceInfo ? $this->priceInfo->FirstWeight : '' ; ?>" name="firstWeight" id="firstWeight" class="txtBxF"><i>g</i>
                            </div>
                            <div class="szbox">
                                <i>首重价</i>
                                <input type="text" datatype="float"  value="<?php echo $this->priceInfo ? $this->priceInfo->firstPrice : '' ; ?>" id="firstPrice" name="firstPrice" class="txtBxF">
                                <i>元</i>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="addl">续重：</td>
                    <td>
                        <div class="addtian2">
                            <input type="text" datatype="float" value="<?php echo $this->priceInfo ? $this->priceInfo->SubsequentWeight : '' ; ?>" name="subsequentWeight" id="subsequentWeight" class="txtBxF"><i>g</i>
                        </div>
                        <div class="xuzhong">
                            <div id="xuzhongman">
                            <?php if($this->priceInfo && $this->priceInfo->Pricingxz){ ?>
                                <?php foreach ($this->priceInfo->Pricingxz as $key => $p) {?>
                                <div class="xzbox" style="width:668px">
                                    <div class="xznr"><i>重量段：</i><input type="text" datatype="float"  value="<?= $p->start ;?>" name="startx[]" mark="startx" class="txtBxF"><i>-</i><input type="text" datatype="float"  value="<?= $p->end ;?>" name="endx[]" mark="endx" class="txtBxF"><i>kg</i></div>
                                    <div class="xznr"><span>续重价：</span><input type="text" datatype="float"  value="<?= $p->price ;?>" name="pricex[]" mark="pricex" class="txtBxF"><i>元</i></div>
                                </div>
                                <?php } ?>
                            <?php }else{ ?>
                                <div class="xzbox" style="width:668px">
                                    <div class="xznr"><i>重量段：</i><input type="text" datatype="float"  value="" name="startx[]" mark="startx" class="txtBxF"><i>-</i><input type="text"  datatype="float" value="" name="endx[]" mark="endx" class="txtBxF"><i>kg</i></div>
                                    <div class="xznr"><span>续重价：</span><input type="text" datatype="float"  value="" name="pricex[]" mark="pricex" class="txtBxF"><i>元</i></div>
                                </div>
                            <?php } ?>
                            </div>
                            <p><a name='butAdditional' href="javascript:;" data='xuzhongman' mark='x'>添加一个重量段</a></p>
                        </div>
                    </td>
                </tr>
            </tbody></table>
        </div>
        <div class="addtable addtable2">
            <table>
                <tbody><tr>
                    <td class="addl">按公斤计费：</td>
                    <td>
                        <div class="addmanbox">
                            <label><input type="radio" value="0" name="isWeight" <?php echo (!$this->priceInfo || $this->priceInfo && $this->priceInfo->IsWeight == 0) ? 'checked' : '' ?>>关闭</label>
                            <label><input type="radio" value="1" name="isWeight" <?php echo $this->priceInfo && $this->priceInfo->IsWeight == 1 ? 'checked' : '' ?>>开启</label>
                            <div class="addman"><span>满</span><input type="text" datatype="float"  value="<?php echo $this->priceInfo ? $this->priceInfo->Beyond : '0' ; ?>" id="beyond" name="beyond" class="txtBxF addwu"><span>公斤，按公斤计费</span></div>
                        </div>
                        <div class="xuzhong" id='gjdiv' style="display:<?php echo $this->priceInfo && $this->priceInfo->isWeight == 1 ? '' : 'none' ; ?>;" >
                            <div id="gongjinman">
                            <?php if($this->priceInfo && $this->priceInfo->Pricinggj){ ?>
                            <?php foreach ($this->priceInfo->Pricinggj as $key => $p) {?>
                                <div class="xzbox" style="width:668px">
                                    <div class="xznr"><i>重量段：</i><input type="text" datatype="float"  value="<?= $p->start ;?>" name="startg[]" mark="startg" class="txtBxF"><i>-</i><input type="text" datatype="float"  value="<?= $p->end ;?>" name="endg[]" mark="endg" class="txtBxF"><i>kg</i></div>
                                    <div class="xznr"><span>每公斤价：</span><input type="text" datatype="float"  value="<?= $p->price ;?>" name="priceg[]" mark="priceg" class="txtBxF"><i>元</i></div>
                                </div>
                                <?php } ?>
                            <?php }else{ ?>
                                <div class="xzbox" style="width:668px">
                                    <div class="xznr"><i>重量段：</i><input type="text" datatype="float"  value="" name="startg[]" mark="startg" class="txtBxF"><i>-</i><input type="text" datatype="float"  value="" name="endg[]" mark="endg" class="txtBxF"><i>kg</i></div>
                                    <div class="xznr"><span>每公斤价：</span><input type="text" datatype="float"  value="" name="priceg[]" mark="priceg" class="txtBxF"><i>元</i></div>
                                </div>
                            <?php } ?>
                            </div>
                            <p><a name='butAdditional' href="javascript:;" data='gongjinman' mark='g'>添加一个重量段</a></p>
                        </div>
                    </td>
                </tr>
            </tbody></table>
        </div>
    </div>
    <div class="addbar">
        <ul>
            <li><a href="javascript:;" class="ccbotton" name="save"><span>保存设置</span></a></li>
            <li><a href="javascript:;" class="ccbotton2" name="reset"><span>重置</span></a></li>
        </ul>
    </div>
    </from>
</div>
<script type="text/javascript">
    $(function(){
        <?php if($this->cityId && is_numeric($this->cityId)){ ?>
        //设置默认国家start
        var cname = $('[cid=<?php echo (int)$this->cityId; ?>]').eq(1).text();
        if(cname == ''){
            cname = '美国';
            $('#cityId').val('1');
        }
        $('#cityName').val(cname);
        //设置默认国家end

        <?php } ?>



        //显示国家
        $('.addressbookinput').hover(function() {
        }, function() {
            // $('.share_guojiaceng').hide();
        }).click(function() {
            $('.share_guojiaceng').toggle();
        });

        //选择国家
        $('.share_countrylist a').bind('click', function() {
            var city  = $(this).text();
            var state = $(this).attr("cid");
            $("#cityId").val(state);
            $("#cityName").val(city);
            $('.share_guojiaceng').hide();
            skip();
            return false;
        });

        //选择快递
        $('#epId').change(function(){
            $('#epIdtext').val($('#epId').val());
            skip();
        })

        //切换国家
        $('.share_countryzm a').each(function(index, item) {
            $(item).click(function() {
                $('.share_countryzm li.share_zmon').removeClass('share_zmon');
                $(this).parent().addClass('share_zmon');
                $('.country_content ul').hide();
                $('.country_content ul').eq(index).show();
                return false;
            });
        });

        //隐藏div
        $('.share_guojiaceng').mouseout(function(e){
            if(isMouseLeaveOrEnter(e, this)){
                $(this).hide();
            }
        })

        //添加一个重量段
        $('[name=butAdditional]').click(function(){
            // var size = $('#xuzhongman .xzbox').size();
            var mark = $(this).attr('mark');
            var name = '续重价';
            if(mark == 'g'){
                name = '每公斤价';
            }
            var tmp  = "<div class='xzbox'  style='width:668px'>"
                     + "<div class='xznr'><i>重量段：</i><input type='text' datatype='float'  value='' name='start"+ mark +"[]' mark='start"+ mark +"' class='txtBxF'><i>-</i><input type='text' datatype='float'  value='' name='end"+ mark +"[]' mark='end"+ mark +"' class='txtBxF'><i>kg</i></div>"
                     + "<div class='xznr'><span>"+ name +"：</span><input type='text' datatype='float'  value='' name='price"+ mark +"[]' mark='price"+ mark +"' class='txtBxF'><i>元</i></div>"
                     + "<div class='del'><a href='javascript:;'>删除</a></div>";
                     + "</div>";

            $('#' + $(this).attr('data')).append(tmp);
            $('[datatype=float]').floattype();
            $("html,body").animate({scrollTop:$("html,body").scrollTop()+60},0);
        })

        //删除
        $('.del').live('click', function(){
            $(this).parent().remove();
            $("html,body").animate({scrollTop:$("html,body").scrollTop()-60},0);
        })

        //重置
        $('[name=reset]').click(function(){
            location.reload();
        })

        function skip()
        {
            var epId   = $('#epIdtext').val();
            var cityId = $("#cityId").val();
            setTimeout(function(){
                location.href = '/admini/express/addprice/epId/' + epId + '/cityId/' + cityId;
            }, 500);
        }

        function check(type)
        {
            var startx, endx, pricex;
            if(type == 1){
                startx    = $('[mark=startx]');
                endx      = $('[mark=endx]');
                pricex    = $('[mark=pricex]');
            }else{
                startx    = $('[mark=startg]');
                endx      = $('[mark=endg]');
                pricex    = $('[mark=priceg]');
            }

            var continued = $('#xuzhongman .xzbox');
            var start     = 0;
            var end       = 0;
            var price     = 0;
            var error     = 0;
            var run       = true;//运行状态
            var temindex  = 0;//临时
            // 续重
            startx.each(function(index, item){
                start = parseFloat($(item).val());
                if(index != 0 && (isNaN(start) || start < 0 || start === '' )){
                    error++;
                    return false;
                }
                if(index != 0 && start != endx.eq(index-1).val()){
                    error++;
                    return false;
                }
            })
            endx.each(function(index, item){
                temindex = index + 1;
                end = parseFloat($(item).val());
                if(endx.size() > 2 ){
                    if( endx.size() != index + 1 && ( isNaN(end) || end < 0 || end === '' || end >= parseFloat(endx.eq(temindex).val()) )){
                        error++;
                        return false;
                    }
                }else{
                    if( endx.size() != index + 1 && ( isNaN(end) || end < 0 || end === '')){
                        error++;
                        return false;
                    }
                }
            })
            pricex.each(function(){
                price = $(this).val();
                if(isNaN(price) || price <= 0 || price === ''){
                    error++;
                    return false;
                }
            })

            if(error > 0){
                run = false;
            }
            return run;
        }

        $('[name=isWeight]').click(function(){
            var val = $(this).val();
            if(val == 1){
                $('#gjdiv').show(566);
            }else{
                $('#gjdiv').hide(566);
            }
        })

        //保存
        $('[name=save]').click(function(){
            var isWeight = $('[name=isWeight]:checked').val();
            var beyond   = $('#beyond').val();
            var run      = check(1) && (isWeight == 0 || (isWeight == 1 && check(2) && beyond != '' )) ;

            if(beyond == ''){
                $('#beyond').val('0');
            }

            var maxkg            = $('[name=maxkg]').val();
            var maxcm            = $('[name=maxcm]').val();
            var firstWeight      = $('[name=firstWeight]').val();
            var firstPrice       = $('[name=firstPrice]').val();
            var subsequentWeight = $('[name=subsequentWeight]').val();

            if(maxkg == '' || maxcm == '' || firstWeight == '' || firstPrice == '' || subsequentWeight == ''){
                alert('请输入完整信息');
                return;
            }

            var tmpval1 = parseFloat($.trim($('[mark=endx]').eq($('[mark=endx]').size() - 1).val()));
            var tmpval2 = parseFloat($.trim($('[mark=startg]').eq(0).val()));

            if(isWeight == 1 && (parseFloat(beyond) != tmpval1 || parseFloat(beyond) != tmpval2)){
                alert('续重结束重量段值应与满足公斤数值和按公斤计费初始值相同');
                alert('现在3个值分别为' + tmpval1 +','+ parseFloat(beyond) +','+ tmpval2)
                return;
            }

            if(run){
                $('#formstore').submit();
            }else{
                alert('数据信息有误');
            }
        })

    })
    //阻止冒泡
    function isMouseLeaveOrEnter(e, handler) 
    {
        if (e.type != 'mouseout' && e.type != 'mouseover') return false;
        var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
        while (reltg && reltg != handler)
            reltg = reltg.parentNode;
        return (reltg != handler);
    }
</script>